package com.zhaosc.rx.example;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.subjects.BehaviorSubject;

public class RxPreLoader<T>{
	
	private BehaviorSubject<T> mData;
	
	private Disposable disposable;

	public RxPreLoader(T defaultValue) {
		super();
		this.mData = BehaviorSubject.createDefault(defaultValue);
	}
	
	public void publish(T data) {
		mData.onNext(data);
	}

	public Disposable subcribe(Consumer onNext) {
		return disposable=mData.subscribe(onNext);
	}
	
	public void disposable() {
		if(disposable!=null&&!disposable.isDisposed()) {
			disposable.dispose();
			disposable=null;
		}
	}
	
	public BehaviorSubject<T> getSubject(){
		return mData;
	}
	
	public T getLastData() {
		return mData.getValue();
	}
	
}
